package org.remixlab.gnutopic.data;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TreeMap;

import org.remixlab.gnutopic.pojo.ForumCategory;
import org.remixlab.gnutopic.pojo.ForumMessage;
import org.remixlab.gnutopic.pojo.ForumThread;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlHandler  extends DefaultHandler {
	String temp = "";
	String tagName = "";
	
	private ArrayList<ForumCategory> categories = new ArrayList<ForumCategory>();
	
	private TreeMap<Date, ForumThread> threads = new TreeMap<Date, ForumThread>();
	
	private boolean bSubCategories = false;
	//boolean bThreads = false;
	
	private ForumCategory mainCategory;
	private ForumCategory subCategory;
	
	private ForumThread thread;
	private ForumMessage message;
	
	private DateFormat formatter = new SimpleDateFormat("MM-dd-yy 'at' HH:mm:ss");
	
	public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException {
		//System.out.println("startElement: "+qName);
		
		tagName = qName;
		
		if(qName.equalsIgnoreCase("category")){
			//si es una categoría y no estoy dentro de la lista de sub categorias.
			if(!bSubCategories){
				//creo la categoría y la agrego al arreglo de categorías.
				mainCategory = new ForumCategory();
				categories.add(mainCategory);
			}else{
				//es una sub categoría.. creo la sub categoría y la agrego a la categoría principal.
				subCategory = new ForumCategory();
				
				//agrego la categoría principal.
				subCategory.setParentCategory(mainCategory);
				
				//agrego la sub-categoría a la lista de categorías.
				mainCategory.addSubCategory(subCategory);
			}
		}
		if(qName.equalsIgnoreCase("subcategories")){
			bSubCategories = true;
		}
		
		if(qName.equalsIgnoreCase("thread")){
			//creo el hilo.
			this.thread = new ForumThread();
			
			if(this.bSubCategories){
				this.thread.setCategory(this.subCategory);
			}
		}
		
		if(qName.equalsIgnoreCase("message")){
			//creo el mensaje.. y lo agrego al hilo
			message = new ForumMessage();
			
			thread.addMessage(message);
		}
		
		//inicializo la cadena donde voy a guardar los datos.
		temp = "";
	}
	
	public void endElement(String uri, String localName, String qName) throws SAXException {
		//System.out.println("endElement: "+qName);
		//System.out.println("string: "+temp);
		
		if(qName.equalsIgnoreCase("subcategories")){
			this.bSubCategories = false;
		}
		
		//datos de la categoría...
		if(qName.equalsIgnoreCase("categoryName")){
			if(!this.bSubCategories){
				this.mainCategory.setName(temp);
			}else{
				this.subCategory.setName(temp);
			}
		}
		if(qName.equalsIgnoreCase("categoryId")){
			if(!this.bSubCategories){
				this.mainCategory.setId(Integer.parseInt(temp));
			}else{
				this.subCategory.setId(Integer.parseInt(temp));
			}
		}
		if(qName.equalsIgnoreCase("categoryMessageNumber")){
			if(!this.bSubCategories){
				this.mainCategory.setMessagesNumber(Integer.parseInt(temp));
			}else{
				this.subCategory.setMessagesNumber(Integer.parseInt(temp));
			}
		}
		
		//Lleno los datos del thread
		if(qName.equalsIgnoreCase("threadId")){
			this.thread.setId(temp);
		}
		if(qName.equalsIgnoreCase("threadTitle")){
			this.thread.setTopic(temp);
		}
		if(qName.equalsIgnoreCase("threadDate")){
			try {
				this.thread.setDate(formatter.parse(temp));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(qName.equalsIgnoreCase("threadUser")){
			this.thread.setUser(temp);
		}
		if(qName.equalsIgnoreCase("thread")){
			if(bSubCategories){
				//agrego el hilo a la sub-categoría.
				subCategory.addThread(thread);
				
				//agrego el hilo al treeMap de hilos del foro.
				threads.put(thread.getDate(), thread);
				
				//agrego el thread a la categoría principal.
				mainCategory.addThread(thread);
			}
		}
		
		//lleno los datos del mensaje...
		if(qName.equalsIgnoreCase("messageId")){
			this.message.setMessageId(Integer.parseInt(temp));
		}
		
		if(qName.equalsIgnoreCase("messageUser")){
			this.message.setUserName(temp);
		}
		if(qName.equalsIgnoreCase("messageText")){
			this.message.setText(temp);
		}
		if(qName.equalsIgnoreCase("messageDate")){
			try {
				this.message.setDate(this.formatter.parse(temp));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public void characters(char ch[], int start, int length) throws SAXException {
		//System.out.println("characters: "+new String(ch, start, length));
		temp = temp + new String(ch, start, length);
	}

	public ArrayList<ForumCategory> getCategories() {
		return categories;
	}

	public void setCategories(ArrayList<ForumCategory> categories) {
		this.categories = categories;
	}

	public TreeMap<Date, ForumThread> getThreads() {
		return threads;
	}

	public void setThreads(TreeMap<Date, ForumThread> threads) {
		this.threads = threads;
	}

}
